<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>主页</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
    <?php include 'includes/navbar.php'; ?>
    <div class="container mt-5">
        <div class="row">
            <div class="col-12">
                <div id="ad-carousel" class="carousel slide" data-bs-ride="carousel">
                    <div class="carousel-inner">
                        <div class="carousel-item active">
                            <img src="/wx.png" class="d-block w-100" alt="广告1">
                            <div class="carousel-caption d-none d-md-block">
                                <h5>公众号</h5>
                                <p>欢迎关注</p>
                            </div>
                        </div>
                        <!--<div class="carousel-item">-->
                        <!--    <img src="https://example.com/ad2.jpg" class="d-block w-100" alt="广告2">-->
                        <!--    <div class="carousel-caption d-none d-md-block">-->
                        <!--        <h5>广告标题2</h5>-->
                        <!--        <p>广告描述2。</p>-->
                        <!--    </div>-->
                        <!--</div>-->
                        <!--<div class="carousel-item">-->
                        <!--    <img src="https://example.com/ad3.jpg" class="d-block w-100" alt="广告3">-->
                        <!--    <div class="carousel-caption d-none d-md-block">-->
                        <!--        <h5>广告标题3</h5>-->
                        <!--        <p>广告描述3。</p>-->
                        <!--    </div>-->
                        <!--</div>-->
                    </div>
                    <button class="carousel-control-prev" type="button" data-bs-target="#ad-carousel" data-bs-slide="prev">
                        <span class="carousel-control-prev-icon" aria-hidden="true"></span>
                        <span class="visually-hidden">上一个</span>
                    </button>
                    <button class="carousel-control-next" type="button" data-bs-target="#ad-carousel" data-bs-slide="next">
                        <span class="carousel-control-next-icon" aria-hidden="true"></span>
                        <span class="visually-hidden">下一个</span>
                    </button>
                </div>
            </div>
        </div>
    </div>
    <div class="container mt-5">
        <h1 class="mb-4">欢迎来到小工具主页</h1>
        <div class="row" id="toolsContainer">
            <!-- 小工具内容 -->
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            fetchTools();
        });
        
        function fetchTools() {
            fetch('admin/get-tools.php')
                .then(response => {
                    if (!response.ok) {
                        throw new Error('网络请求失败');
                    }
                    return response.json();
                })
                .then(data => {
                    if (data.success) {
                        var toolsContainer = document.getElementById('toolsContainer');
                        toolsContainer.innerHTML = '';
                        data.tools.forEach(function(tool) {
                            if (tool.status === '1') { // 确保 status 是字符串类型
                                var toolCard = `
                                    <div class="col-md-6">
                                        <div class="card mb-4">
                                            <div class="card-header">
                                                ${tool.name}
                                            </div>
                                            <div class="card-body">
                                                <p>${tool.description}</p>
                                                <p>调用价格: ${tool.price} 元/次</p>
                                                <button type="button" class="btn btn-primary" onclick="useTool(${tool.id})">使用工具</button>
                                            </div>
                                        </div>
                                    </div>
                                `;
                                toolsContainer.innerHTML += toolCard;
                            }
                        });
                    } else {
                        document.getElementById('toolsContainer').innerHTML = '<p>暂无可用小工具</p>';
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    document.getElementById('toolsContainer').innerHTML = '<p>加载小工具失败，请检查网络连接</p>';
                });
        }
        
        function useTool(toolId) {
            if (!sessionStorage.getItem('isLoggedIn')) {
                window.location.href = 'login.html';
                return;
            }
        
            // 确保 toolId 是数字类型
            toolId = parseInt(toolId);
        
            // 根据工具ID调用相应的API
            switch (toolId) {
                case 1:
                    window.location.href = 'verify.html';
                    break;
                case 6:
                    window.location.href = 'mobile-verify.html';
                    break;
                case 7:
                    window.location.href = 'xmbx.php';
                    break;
                case 8:
                    window.location.href = 'remove-watermark.php';
                    break;
                case 9:
                    window.location.href = 'wxy.php';
                    break;
                case 10:
                    window.location.href = 'short-movie.php';
                    break;
                default:
                    alert('工具不存在');
            }
        }
    </script>
</body>
</html>